package com.example.testgit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.testgit.VO.CourseVO;
import com.example.testgit.VO.PageResult;
import com.example.testgit.VO.ResearchVO;
import com.example.testgit.VO.StudentQuery;
import com.example.testgit.entity.CourseDO;
import com.example.testgit.entity.ResearchDO;
import com.example.testgit.mapper.CourseMapper;
import com.example.testgit.mapper.ResearchMapper;
import com.example.testgit.mapstruct.CourseConverter;
import com.example.testgit.mapstruct.ResearchConverter;
import com.example.testgit.service.CourseResearchService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lyh
 * @create: 2023/5/8 10:31
 * Description：
 */
@Service
public class CourseResearchServiceImpl implements CourseResearchService {

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private ResearchMapper researchMapper;

    @Override
    public PageResult<CourseVO> queryScore(StudentQuery stu) {
        QueryWrapper<CourseDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stu.getStuId());
        Page<CourseDO> page = new Page<>(stu.getPageNum(), stu.getPageSize());

        Page<CourseDO> courseDOPage = courseMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(courseDOPage);
        pageResult.setData(CourseConverter.INSTANCE.toVOList(courseDOPage.getRecords()));
        for (CourseVO course : (List<CourseVO>)pageResult.getData()) {
            if (course.getIsCount() == 0) course.setCourseScore(null);
        }

        // 分绩点的计算
        // 过滤isCount = 1的课程
        List<CourseDO> compute = courseMapper.selectList(wrapper).stream().filter(obj -> obj.getIsCount() == 1).collect(Collectors.toList());
        int credit = 0;
        int totalClass = compute.size();
        int passClass = compute.size();
        int totalScore = 0;
        for (CourseDO course : compute) {
            if (course.getCourseScore() < 60) passClass--;
            else credit += course.getCourseCredit();
            totalScore += course.getCourseScore();
        }
        double passingRate = passClass * 1.0 / totalClass;
        double gpa = totalScore * 1.0 / totalClass / 100 * 4;
        JSONObject json = new JSONObject();
        json.put("credit", credit);
        json.put("passingRate", passingRate);
        json.put("gpa", Double.parseDouble(String.format("%.2f",gpa)));
        pageResult.setNote(json);

        return pageResult;
    }

    @Override
    public PageResult<ResearchVO> queryResearch(StudentQuery stu) {
        QueryWrapper<ResearchDO> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", stu.getStuId());
        Page<ResearchDO> page = new Page<>(stu.getPageNum(), stu.getPageSize());

        Page<ResearchDO> researchDOPage = researchMapper.selectPage(page, wrapper);
        PageResult pageResult = new PageResult(researchDOPage);
        pageResult.setData(ResearchConverter.INSTANCE.toVOList(researchDOPage.getRecords()));

        // 成果类别的计算
        List<ResearchDO> compute = researchMapper.selectList(wrapper);
        int paper = 0;
        int literature = 0;
        int patent = 0;
        int soft = 0;
        for (ResearchDO res : compute) {
            if (res.getOutcomeType() == 0) paper++;
            else if (res.getOutcomeType() == 1) literature++;
            else if (res.getOutcomeType() == 2) patent++;
            else soft++;
        }
        JSONObject json = new JSONObject();
        json.put("paper", paper);
        json.put("literature", literature);
        json.put("patent", patent);
        json.put("soft", soft);
        pageResult.setNote(json);

        return pageResult;
    }
}
